import React, { Fragment } from 'react';
import { Empty, Statistic, Row, Col } from 'antd';
import ReactEcharts from '@/components/Echarts';
import styles from '../index.less';
// 后端没有返回图例name  前端写死渲染
const getPassOption = data => {
    const { dataY, timeX } = data;
    const option = {
        tooltip: {
            formatter: ' {b0}<br/>{a0}：{c0}% <br/>{a1}：{c1}% <br/>{a2}：{c2}% ',
            backgroundColor: '#485465',
            padding: [3, 9, 3, 9],
            textStyle: {
                fontSize: 12
            },
            trigger: 'axis'
        },
        legend: {
            bottom: 10,
            itemGap: 16,
            data: ['离职率', '主动离职率', '被动离职率']
        },
        grid: {
            left: '11%',
            bottom: 80
        },
        xAxis: {
            type: 'category',
            data: timeX,

            boundaryGap: true,
            axisPointer: {
                type: 'shadow',
                shadowStyle: {
                    color: 'rgba(213, 213, 213, 0.2)'
                }
            },
            axisLine: {
                lineStyle: {
                    color: '#D9D9D9'
                }
            },
            axisTick: {
                alignWithLabel: true,
                interval: 0
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)',
                interval: 0,
                formatter(value, index) {
                    if (index === dataY[0].data.length - 1) {
                        return `{cur|${value}}`;
                    }
                    return value;
                },
                rich: {
                    cur: {
                        color: '#FFA22D',
                        fontSize: 12
                    }
                }
            }
        },
        yAxis: {
            type: 'value',
            name: '单位：%',
            nameTextStyle: {
                fontSize: 12,
                align: 'right',
                color: 'rgba(0, 0, 0, 0.65)',
                fontWeight: 500
            },
            // min: 90.5,
            // max: 96,
            // interval: 1,
            splitLine: {
                lineStyle: {
                    color: '#E8E8E8',
                    type: 'dashed'
                }
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.65)',
                formatter(v) {
                    return v.toFixed(2);
                }
            }
        },
        series: dataY
    };

    return option;
};

const Lzl = ({ data: passData }) => {
    // 后端返回数据格式转换
    // const jsonChange = jsonData => {
    const { activeLeaveList = [], leaveList = [], unActiveLeaveList = [] } = passData;
    const lzlArrX = [];
    const zdlzy = [];
    activeLeaveList.forEach(item => {
        lzlArrX.push(item.yearMonth);
        if (item.totalCount === 0) {
            zdlzy.push(0);
        } else {
            zdlzy.push(Number(((item.activeLeftCount / item.totalCount) * 100).toFixed(2)));
        }
    });
    const lzly = leaveList.map(_ => {
        if (_.totalCount !== 0) {
            return Number(((_.leftCount / _.totalCount) * 100).toFixed(2))
        }
        return 0
    }
    );
    const bdlzy = unActiveLeaveList.map(_ => {
        if (_.totalCount !== 0) {
            return Number(((_.unActiveLeftCount / _.totalCount) * 100).toFixed(2))
        }
        return 0
    });
    const newData = {
        timeX: lzlArrX,
        dataY: [
            {
                name: '离职率',
                data: lzly,
                type: 'line'
            },
            {
                name: '主动离职率',
                data: zdlzy,
                type: 'line'
            },
            {
                name: '被动离职率',
                data: bdlzy,
                type: 'line'
            }
        ]
    }
    // return newData;
    // }

    return (
        <Fragment>
            <Row gutter={16}>
                <Col span={8}>
                    <Statistic title="新增已离职员工：" value={passData.leftCount} suffix=" 人" />
                </Col>
                {/* <Col span={8}>
                        <Statistic title="离职中员工：" value={5} suffix=" 人" />
                    </Col> */}
            </Row>
            <div className={styles.chartTitle}>离职率</div>
            {!passData || passData.length === 0 ? (
                <Empty style={{ margin: '100px 0' }} image={Empty.PRESENTED_IMAGE_SIMPLE} />
            ) : (
                <ReactEcharts style={{ height: 370 }} option={getPassOption(newData)} />
            )}
        </Fragment>
    );
};

export default Lzl;
